package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/business/statementItem")
public class StatementItemController {

    @Autowired
    private IStatementService statementService;

    @Autowired
    private IStatementItemService statementItemService;

    private static final String prefix = "business/statementItem/";

    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        return new TablePageInfo<>(statementItemService.selectForList(qo));
    }

    @RequestMapping("/saveItems")
    @ResponseBody
    //@RequestBody => 用于接收前端请求过来的JSON数据
    public AjaxResult saveItems(@RequestBody List<StatementItem> list) {
        statementItemService.saveItems(list);
        return AjaxResult.success();
    }

    @RequestMapping("/itemDetail")
    public String itemDetail(Model model ,Long statementId) {
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement",statement);
        return statement.getStatus().equals(Statement.STATUS_CONSUME) ? prefix + "itemDetail" : prefix + "itemPayed";
    }

    @RequestMapping("/pay")
    @ResponseBody
    //@RequestBody => 用于接收前端请求过来的JSON数据
    public AjaxResult pay(Long statementId) {
        statementItemService.pay(statementId);
        return AjaxResult.success();
    }

}
